<template>
    <view class="contant">
        <view class="top_view">请上传您的营业执照</view>
         <view class="add_pics">
              <view class="left-text" @click="chooseImage">
                <view class="name">营业执照</view>
                <view class="myfont">上传您的营业执照</view>
              </view>
                <view class="showPic">
                    <image class="id_front" :src="form.licenseImage || $mConfig.imageUrl + '/image/yyzz.png'" mode="aspectFit"></image>
                </view>
        </view>
        <view class="top_view" style="margin:42rpx 0 38rpx">请填写内容信息</view>
        <view class="cell_view">
            <view class="input_view">
                <view class="left_name">公司名称</view>
                <input class="input" placeholder="请输入公司名称" @input="nameChange" v-bind:value="form.name" placeholder-style="color: #D2D2D2;font-size: 32rpx;" />
            </view>
        </view>
         <view class="cell_view">
            <view class="input_view">
                <view class="left_name">经营者姓名</view>
                <input class="input" placeholder="请输入经营者姓名" @input="nameChange" v-bind:value="form.name" placeholder-style="color: #D2D2D2;font-size: 32rpx;" />
            </view>
        </view>

        <view class="cell_view">
            <view class="input_view">
                <view class="left_name">统一组织代码号</view>
                <input class="input" placeholder="请输入代码号" @input="idChange" v-bind:value="form.id" placeholder-style="color: #D2D2D2;font-size: 32rpx;" />
            </view>
        </view>
        <view class="cell_view">
            <view class="input_view">
                <view class="left_name">经营场所</view>
                <input class="input" @input="codePhoneChange" v-bind:value="form.phone" placeholder-style="color: #D2D2D2;font-size: 32rpx;" placeholder="请选择经营地址" />
            </view>
        </view>
       
        

       
         
        <!-- <view v-if="submitBtn" class="submit" @click="submit">确认提交</view> -->
        <bottom-button  :isFixed="true" @sendFun="submit" text="下一步"></bottom-button>
    </view>
</template>

<script>
import { postOcr } from '@/apis/index.js'
// import {
//     postLoginRegister,
//     getSendSms
// } from '../../../../services/api.js';
// import graceChecker from '../../../../utils/graceChecker.js';
export default {
    onLoad() {
        // this.fetch();
    },
    data() {

        return {
            form: {
                name: '',
                id: '',
                phone: '',
                code: '',
                licenseImage: '',
                licenseBase64: '',
            },
            selete: false,

            showText: true,
            second: 59,
            disabled: false,
            changess: true,
            submitBtn: true,
        }
    },
    methods: {
        async fetch() {
            // const res = await getBankCardList(0);
            // console.log(JSON.stringify(res));
            // if(res.code == 200){
            // 	this.cardList = res.result.records;
            // }else{
            // 	uni.showToast({
            // 		title: res.message,
            // 		icon: 'none'
            // 	})
            // }

        },
        changeBtn() {
            this.selete = !this.selete;
        },
        nameChange(e) {
            this.name = e.detail.value;
        },
        idChange(e) {
            this.id = e.detail.value;
        },
        phoneChange(e) {
            this.phone = e.detail.value;
        },
        codeChange(e) {
            this.code = e.detail.value;
        },
        async submit() {
            if (!this.form.licenseImage) {
                uni.showToast({
                    title: '请上传营业执照',
                    icon: 'none'
                });
                return;
            }
            
            if (!this.form.name) {
                uni.showToast({
                    title: '请输入公司名称',
                    icon: 'none'
                });
                return;
            }
            
            if (!this.form.id) {
                uni.showToast({
                    title: '请输入统一社会信用代码',
                    icon: 'none'
                });
                return;
            }
            
            uni.navigateTo({
                url: '/pages/user/renzheng/underReview'
            });
        },
        async chooseImage() {
            try {
                const res = await uni.chooseImage({
                    count: 1,
                    sourceType: ['album', 'camera'],
                    sizeType: ['compressed']
                });
                console.log(JSON.stringify(res));

                // res是一个数组，第二个元素才是包含图片信息的对象
                const imageInfo = res[1];
                if (!imageInfo || !imageInfo.tempFilePaths || imageInfo.tempFilePaths.length === 0) {
                    throw new Error('未选择图片');
                }
                
                const tempFile = imageInfo.tempFilePaths[0];
                
                uni.showLoading({ title: '处理中...' });
                
                const base64 = await this.imageToBase64(tempFile);
                
                this.form.licenseImage = tempFile;
                this.form.licenseBase64 = base64;
                
                await this.recognizeLicense(base64);
                
            } catch (error) {
                console.error('选择图片失败:', error);
                uni.showToast({
                    title: '选择图片失败',
                    icon: 'none'
                });
            } finally {
                uni.hideLoading();
            }
        },
        
        imageToBase64(filePath) {
            return new Promise((resolve, reject) => {
                try {
                    const fs = uni.getFileSystemManager();
                    fs.readFile({
                        filePath,
                        encoding: 'base64',
                        success: (res) => {
                            console.log('读取文件成功');
                            resolve(res.data);
                        },
                        fail: (err) => {
                            console.error('读取文件失败:', err);
                            reject(err);
                        }
                    });
                } catch (error) {
                    console.error('文件系统错误:', error);
                    reject(error);
                }
            });
        },
        
        async recognizeLicense(base64) {
            try {
                const res = await postOcr({
                    base64: base64,
                    options: {
                        "ocr.language": "models/config_chinese.txt",
                        "ocr.cls": true,
                        "ocr.limit_side_len": 4320,
                        "tbpu.parser": "multi_none",
                        "data.format": "text"
                    }
                });
                
                if (res.code === 200) {
                    const data = res.data;
                    this.form.name = data.companyName || '';
                    this.form.id = data.creditCode || '';
                    this.form.phone = data.address || '';
                    
                    uni.showToast({
                        title: '识别成功',
                        icon: 'success'
                    });
                } else {
                    throw new Error(res.message || '识别失败');
                }
            } catch (error) {
                console.error('OCR识别失败:', error);
                uni.showToast({
                    title: error.message || '识别失败',
                    icon: 'none'
                });
            }
        },
        isPhone(value) {
            if (!/^1(3|4|5|6|7|8|9)\d{9}$/.test(value)) {
                return false
            } else {
                return true
            }
        },
        async codeClick() {
            // console.log('验证码');
            if (this.phone == '') {
                uni.showToast({
                    title: '请输入手机号',
                    icon: 'none'
                })
                return;
            }
            if (this.isPhone(this.phone) == false) {
                uni.showToast({
                    title: '手机号格式不正确',
                    icon: 'none'
                });
                return;
            }
            if (this.disabled == true) {
                return;
            }
            const res = await getSendSms(this.phone, 1);
            console.log(JSON.stringify(res));
            if (res.code == 200) {
                this.changess = false;
                this.disabled = true;
                this.showText = false;
                var interval = setInterval(() => {
                    this.second = this.second - 1;
                }, 1000);

                setTimeout(() => {
                    clearInterval(interval);
                    this.showText = true
                    this.second = 59
                    this.disabled = false
                    this.changess = true;
                }, 60000);
            } else {
                uni.showToast({
                    title: res.message,
                    icon: 'none'
                })
            }
        }
    }
}
</script>

<style lang="scss" scoped>
.contant{
  padding: 20rpx 30rpx 0;
  color: #222222;
 .top_view {
      font-size: 36rpx;
      font-weight: bold;
  }
  .input_view {
    height: 110rpx;
    display: flex;
    align-items: center;
    justify-content: space-between;
    position: relative;
    border-bottom: 1px solid #F1F1F1;
  }
   .left_name {
      font-size: 30rpx;
      font-weight: 600;
      position: absolute;
    }

    .input {
        font-size: 30rpx;
        font-weight: 400;
        color: #999;
        margin-left: 160rpx;
        text-align: right;
        width: 100%;
    }

    .code_btn {
        font-size: 26rpx;
        color: #fff;
        margin-left: 100rpx;
        background: #3A92FF;
        width: 184rpx;
        line-height: 50rpx;
        border-radius: 25rpx;
        text-align: center;
    }
    .add_pics{
      background: #F5F5FC;
      color: #999999;
      font-size: 28rpx;
      display: flex;
      justify-content: space-between;
      padding: 36rpx;
      margin:46rpx 0;
      border-radius: 10rpx;
      .left-text{
        .name{
          color: #222;
          font-weight: bold;
          font-size: 36rpx;
        }
        .myfont {
            font-size: 28rpx;
            color: #999;
            margin-top: 26rpx;
        }
      }
    }
    .id_front {
        width: 322rpx;
        height: 212rpx;
    }
}
   
</style>
